<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 我的
 */

namespace app\api\controller\user\v1_0\bus;
use Endroid\QrCode\QrCode;

use think\Db;
class User extends \app\api\controller\user\v1_0\Common
{
    protected $noNeedRight = ['test'];
     /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 我的
     */
    public function index(){
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $bus = (new \app\common\model\Bus())->where(['id' => $user['bus_id']])->find();
        return self::successResult("成功",$bus);
    }
    
     /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 生成二维码
     */
    public function ewm(){
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $bus = (new \app\common\model\Bus())->where(['id' => $user['bus_id']])->find();
        if(empty($bus['ewm'])){
            $qrCode = new QrCode("https://xcx.htxxkj.net?type=bus_ewm&bus_id=".$bus['id']);
            $qrCode->setSize(300);
            $qrCode->setMargin(10);
            $dir = './uploads/ewm/'.date('Ymd').'/';
            if(is_dir($dir) === false){
                mkdir($dir);
            }
            $files = date("Y-m-dH-i-s").rand(1000,9999);
         
            $file = $dir.$files.'.png';
            $qrCode->writeFile($file);
            $ewm = '/uploads/ewm/'.date('Ymd').'/'.$files.'.png';
            $data = ((new \app\common\controller\Upload())->COSUploadFile('ewm/' . date('Ymd') . '/' .$files.'.png','http://'.$_SERVER['HTTP_HOST'].$ewm));
            if($data['state'] == 1){
                $bus->ewm = $data['url'];
                $bus->save();
            }
        }
        $bus = (new \app\common\model\Bus())->where(['id' => $user['bus_id']])->find();
        return self::successResult("成功",$bus['ewm']);
    }
   

}
